package com.inda.lemurshooter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class HighScoreActivity extends Activity{
	
	private static final String HIGHSCORE_DATABASE = "highscore";
	private static final int MAX_HIGHSCORES = 5;
	private SharedPreferences highscore;
	private Editor editor;
	private boolean enterHighscore = false;
	private int points;
	private ArrayList<HighscoreEntry> entries;
	private EditText nameField;
	private View button;
	private  SoundPool aSoundPool; //
	private int success, loser;
	private SharedPreferences settings;
	private boolean soundOn;
	
	
	private class HighscoreEntry implements Comparable<HighscoreEntry>
	{
		String name;
		int score;
		
		public HighscoreEntry(String name, int score)
		{
			this.name = name;
			this.score = score;
		}
		
		@Override
		public int compareTo(HighscoreEntry another) {
			if(score < another.score)
				return -1;
			else if(score > another.score)
				return 1;
			else
				return 0;
		}


		public int getScore() {
			return score;
		}
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		aSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
		
		
		Intent i = getIntent();
        Bundle bundle = i.getExtras();
        points = bundle.getInt("score");
		
		
		highscore = getSharedPreferences(HIGHSCORE_DATABASE, Context.MODE_PRIVATE);
		editor = highscore.edit();
		
		settings = this.getSharedPreferences("settings", Context.MODE_PRIVATE);
		soundOn=settings.getBoolean("sounds", true);
				
		
		HashMap<String, Integer> a = (HashMap<String, Integer>) highscore.getAll();
		entries = new ArrayList<HighscoreEntry>();
		for(String key : a.keySet())
		{
			entries.add(new HighscoreEntry(key, a.get(key)));
		}
		
		if(entries.size() < MAX_HIGHSCORES)
		{
			//entries.add(new HighscoreEntry(askForName(), points));
			enterHighscore = true;
		}
		else
		{
			// if current score is higher than any entry in the list then add it
			for(HighscoreEntry entry : entries)
			{
				if(points > entry.getScore())
				{
					//entries.add(new HighscoreEntry(askForName(), points));
					enterHighscore = true;
				}
			}
		}
		
		//HEHE (just for testing of course)
		entries.add(new HighscoreEntry("anonAnna", 21));
		entries.add(new HighscoreEntry("->Aron<-", 9165907));
		entries.add(new HighscoreEntry("Agata", 54));
		entries.add(new HighscoreEntry("Agafsfsdta", 24));
		entries.add(new HighscoreEntry("Aga4535ta", 544));
		//END HEHE
		
		setContentView(R.layout.highscore_layout);
		if(enterHighscore)
		{
			success = aSoundPool.load(this, R.raw.success, 1);
			nameField = (EditText)findViewById(R.id.nameField);
			nameField.setVisibility(View.VISIBLE);
			button = (View)findViewById(R.id.enterButton);
			button.setVisibility(View.VISIBLE);
			if(soundOn)
			{
				while(aSoundPool.play(success, 1, 1, 1, 0, 1) == 0)
				{
					try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
					}
				}
			}
			
		}
		else
		{
			if(soundOn)
			{
				loser = aSoundPool.load(this, R.raw.looosersound, 1);
				while(aSoundPool.play(loser, 1, 1, 1, 0, 1) == 0)
				{
					try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
					}
				}
			}
			showHighscores();
		}
	}
	
	private void showHighscores()
	{
		// Sort
		Collections.sort(entries); // Ascending
		if(entries.size() > MAX_HIGHSCORES) // Remove the outdated low scores that no longer make the list
		{
			entries.remove(0); // Lowest index = lowest score since it's sorted in ascending order
			editor.remove(entries.get(0).name);
		}
		Collections.reverse(entries); // Arrange the list in descending order

		editor.commit();
		
		// NOW SHOW THE HIGHSCORES
		StringBuilder s = new StringBuilder();
		for(HighscoreEntry entry : entries)
		{
			s.append(entry.name+" "+entry.score+"\n");
		}
		EditText listField = (EditText)findViewById(R.id.list);
		listField.setVisibility(View.VISIBLE);
		listField.setText(s.toString());
	}
	
	
	
	public void submitName(View v)
	{
		String name = nameField.getText().toString();
		entries.add(new HighscoreEntry(name, points));
		editor.putInt(name, points);
		nameField.setVisibility(View.INVISIBLE);
		button.setVisibility(View.INVISIBLE);
		showHighscores();
	}
	
	

}
